/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.HtmlItem;
import com.jb.web.bean.ListBean;
import com.jb.web.bean.TableCell;
import com.jb.web.bean.TableRow;
import com.jb.web.context.WebContext;

import com.jb.faces.component.UIComponent;


public class GridListRenderer extends RendererImpl {
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        if (!isEditable(pCtx, (HtmlItem) itemToRender)) {
            return super.render(pCtx, itemToRender);
        } else {
            return renderDesignTime(pCtx, itemToRender);
        }
    }

    public StringBuffer renderBegin(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer();
        ListBean hItem = (ListBean) itemToRender;
        html.append("<table ")
            .append(getEditScript(pCtx, hItem))
            .append(addTagProperty("style", hItem.getStyle(pCtx), null))
            .append(addTagProperty("class", hItem.getStyleClass(), null))
            .append(addTagProperty("width", "100%", null)).append(">");

        if (hItem.isShowHeader() && (hItem.getHeader(pCtx) != null)) {
            html.append(hItem.getHeader(pCtx).render(pCtx));
        }
        return html;
    }

    public StringBuffer renderChildren(WebContext pCtx, UIComponent itemToRender) {
        ListBean hItem = (ListBean) itemToRender;
        StringBuffer html = new StringBuffer();

        int size = hItem.getSize();

        int cols = hItem.getColumns();

        int iStartAt = hItem.getStart();
        int iEndAt = iStartAt + hItem.getBlockSize();

        if (iEndAt > size) {
            iEndAt = size;
        }

        TableRow row1 = hItem.getRow(0);
        TableRow row2 = hItem.getRow(1);

        TableRow row = row1;
        int oldSeq = hItem.getSequence();
        int rowno = 0;
        int i = 0;

        while (i < (iEndAt - iStartAt)) {
            row = row1;

            if (((rowno % 2) == 1) && (row2 != null)) {
                row = row2;
            }

            row.setClientId("r" + hItem.getId() + "_" + i);

            html.append("<tr>");

            TableCell cell = row.getCellAt(0);
            int width = 100 / cols;
            cell.setWidth("" + width + "%");

            for (int k = 0; k < cols; k++) {
                if (i < (iEndAt - iStartAt)) {
                    
                    hItem.setCurrentRow(hItem.get(iStartAt + i));

                    hItem.setSequence(i);
                    html.append(cell.render(pCtx));
                    i++;
                } else {
                    html.append("<td width=\"" + width + "%\"/>");
                }
            }

            html.append("</tr>");
            hItem.setCurrentRow(null);
            rowno++;
        }

        hItem.setSequence(oldSeq); //reset sequence here

        return html;
    }

    public StringBuffer renderDesignTime(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer();
        ListBean hItem = (ListBean) itemToRender;

        html.append("<TABLE ").append(getEditScript(pCtx, hItem)).append(">");

        if (hItem.isShowHeader() && (hItem.getHeader(pCtx) != null)) {
            html.append(hItem.getHeader(pCtx).render(pCtx));
        }

        for (int i = 0; i < hItem.getRows(); i++) {
            try {
                TableRow row = hItem.getRow(i);
                html.append(row.render(pCtx));
            } catch (Exception e) {
                pCtx.addErrorMessage(hItem, e.getMessage());
            }
        }

        if (hItem.isShowFooter() && (hItem.getFooter(pCtx) != null)) {
            html.append(hItem.getFooter(pCtx).render(pCtx));
        }

        if (hItem.getRows() == 0) {
            html.append("<tr><td>At least one row for a list</td></tr> ");
        }

        html.append("</Table>");

        return html;
    }

    public StringBuffer renderEnd(WebContext pCtx, UIComponent itemToRender) {
        ListBean hItem = (ListBean) itemToRender;
        StringBuffer html = new StringBuffer();

        if (hItem.isShowFooter() && (hItem.getFooter(pCtx) != null)) {
            html.append(hItem.getFooter(pCtx).render(pCtx));
        }

        html.append("</Table>");

        return html;
    }
}
